package com.team.mp.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.mp.api.service.intf.DoctorService;
import com.team.mp.mapper.dao.DocterTeamDao;
import com.team.mp.mapper.dao.DoctorDao;
import com.team.mp.mapper.dao.SignRecordDao;
import com.team.mp.mapper.dao.TeamDao;
import com.team.mp.mapper.dto.DoctorDetailDto;
import com.team.mp.mapper.entity.DocterTeam;
import com.team.mp.mapper.entity.Doctor;
import com.team.mp.mapper.entity.SignRecord;
import com.team.mp.mapper.entity.Team;
import com.team.mp.vo.R;

import javax.annotation.Resource;

import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 千锋郑州Java学科欢迎你
 *
 * @program: Tproject
 * @description: 业务接口实现层
 * @author: xiaopi
 * @create: 2023-03-27 11:47:49
 */
@Service
public class DoctorServiceImpl implements DoctorService {
    /**
     * 注入持久层对象
     */
    @Resource
    private DoctorDao dao;
    @Resource
    private DocterTeamDao docterTeamDao;
    @Resource
    private TeamDao teamDao;
    @Resource
    private SignRecordDao signRecordDao;

    /**
     * 新增内容
     */
    @Override
    public R save(Doctor doctor) {
        if (doctor != null) {
            int i = dao.insert(doctor);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，新增异常！");
    }

    /**
     * 修改内容
     */
    @Override
    public R update(Doctor doctor) {
        if (doctor != null) {
            int i = dao.updateById(doctor);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，修改异常！");
    }

    /**
     * 删除
     */
    @Override
    public R del(long id) {
        if (id > 0) {
            int i = dao.deleteById(id);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
     * 查询详情
     */
    @Override
    public R queryById(long id) {
        DoctorDetailDto dto = new DoctorDetailDto();
        Doctor doctor = dao.selectById(id);
        dto.setDoctor(doctor);
        List<Team> teamList = new ArrayList<>();
        Long totalSignedNum = 0L;
        List<DocterTeam> docterTeamList = docterTeamDao.selectList(new QueryWrapper<DocterTeam>().eq("docter_id", doctor.getId()));
        for (DocterTeam docterTeam : docterTeamList) {
            Team team = teamDao.selectById(docterTeam.getTeamId());
            totalSignedNum += signRecordDao.selectCount(new QueryWrapper<SignRecord>().eq("tid", team.getId()));
            teamList.add(team);
        }
        dto.setTeamList(teamList);
        dto.setSignedNum(totalSignedNum);

        return R.ok(dto);
    }

    /**
     * 查询 所有数据
     */
    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }

    @Override
    public R listByCond(Integer page, Integer size, Integer partId, Integer teamId) {
        return R.ok(dao.listByCond(new Page<>(page, size), partId, teamId));
    }
}
